{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实验题目 3：四阶龙格——库塔方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 问题分析\n",
    "\n",
    "> 准确描述并总结出实验题目（摘要），并准确分析原题的目的和意义。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给定常微分方程初值问题：\n",
    "\n",
    "$$\n",
    "\\left\\{\n",
    "  \\begin{align*}\n",
    "    \\frac{dy}{dx} &= f(x,y), & a \\le x \\le b \\\\\n",
    "    y(a) &= \\alpha, & h = \\frac{b-a}{N}\n",
    "  \\end{align*}\n",
    "\\right.\n",
    "$$\n",
    "\n",
    "求其数值解 $y_n,n=1,2,\\cdots,N$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实验目的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**输入：** $a,b,\\alpha,N$\n",
    "\n",
    "**输出：** 初值问题的数值解 $x_n,y_n,n=0,1,2,\\cdots,N$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数学原理\n",
    "\n",
    "> 数学原理表达清晰且书写准确。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "记 $x_n = a + n\\times h$, $n = 0,1,\\cdots,N$，利用四阶龙格——库塔方法：\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "K_1 &= hf(x_n,y_n) \\\\\n",
    "K_2 &= hf(x_n + \\frac{h}{2} + y_n + \\frac{K_1}{2}) \\\\\n",
    "K_3 &= hf(x_n + \\frac{h}{2}, y_n+\\frac{K_2}{2}) \\\\\n",
    "K_4 &= hf(x_n + h, y_n + K_3) \\\\\n",
    "y_{n+1} &= y_n + \\frac{1}{6}(K_1+2K_2+2K_3+K_4) \\\\\n",
    "&& n = 0,1,\\cdots,{N-1}\n",
    "\\end{align*}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "即可逐次求出微分方程初值问题的数值解 $x_n,y_n,n=0,1,2,\\cdots,N$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 程序设计流程\n",
    "\n",
    "> 编译通过，根据输入能得到正确输出。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引入需要的包\n",
    "\n",
    "from typing import *\n",
    "import numpy as np\n",
    "from pandas import DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 四阶龙格——库塔方法\n",
    "def runge_kutta(\n",
    "        f: Callable[[float, float], float],\n",
    "        a: float, b: float, alpha: float, N: int):\n",
    "    x_list, y_list = [], []\n",
    "    h = (b - a) / N\n",
    "    x, y = a, alpha\n",
    "    x_list.append(x)\n",
    "    y_list.append(y)\n",
    "    for _ in range(N):\n",
    "        k_1 = h*f(x, y)\n",
    "        k_2 = h*f(x+h/2, y+k_1/2)\n",
    "        k_3 = h*f(x+h/2, y+k_2/2)\n",
    "        k_4 = h*f(x+h, y+k_3)\n",
    "        x = x + h\n",
    "        y = y + (k_1 + 2*k_2 + 2*k_3 + k_4) / 6\n",
    "        x_list.append(x)\n",
    "        y_list.append(y)\n",
    "    return x_list, y_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 运行测试参数\n",
    "global_args = [\n",
    "    [lambda x, y: x + y, 0, 1, -1, [5, 10, 20], lambda x: -x-1, \"问题 1 (1)\"],\n",
    "    [lambda x, y: -y**2, 0, 1, 1, [5, 10, 20],\n",
    "        lambda x: 1 / (x + 1), \"问题 1 (2)\"],\n",
    "    [lambda x, y: 2 * y / x + x**2 +\n",
    "        np.exp(x), 1, 3, 0, [5, 10, 20], lambda x: x**2 * (np.exp(x) - np.e), \"问题 2 (1)\"],\n",
    "    [lambda x, y: (y + y**2) / x, 1, 3, -2, [5, 10, 20],\n",
    "     lambda x: 2 * x / (1 - 2 * x), \"问题 2 (2)\"],\n",
    "    [lambda x, y: -20 * (y-x*2) + 2 * x, 0, 1, 1.0 / 3,\n",
    "     [5, 10, 20], lambda x: x**2 + np.exp(-20*x)/3, \"问题 3 (1)\"],\n",
    "    [lambda x, y: -20 * y + 20 *\n",
    "        np.sin(x) + np.cos(x), 0, 1, -1, [5, 10, 20], lambda x: np.exp(-20*x) + np.sin(x), \"问题 3 (2)\"],\n",
    "    [lambda x, y: -20*(y-np.exp(x)*np.sin(x)) + np.exp(x)*(np.sin(x) + np.cos(x)),\n",
    "     0, 1, 0, [5, 10, 20], lambda x: np.exp(x)*np.sin(x), \"问题 3 (3)\"]\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 求数据的均方误差\n",
    "def get_error(f: Callable[[float], float], data):\n",
    "\tx, y = data\n",
    "\tstandard = np.array([f(x_i) for x_i in x])\n",
    "\treturn sum((y - standard) ** 2) / len(x)\n",
    "\t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 运行一次\n",
    "def run(index: int):\n",
    "    res = []\n",
    "    for n in global_args[index][-3]:\n",
    "        data = runge_kutta(*[\n",
    "            *global_args[index][:-3], n\n",
    "        ])\n",
    "        error = get_error(global_args[index][-2], data)\n",
    "        res.append({\n",
    "            \"N\": n,\n",
    "            \"标号\": global_args[index][-1],\n",
    "            \"均方误差\": error,\n",
    "            \"x\": data[0],\n",
    "            \"y\": data[1],\n",
    "        })\n",
    "    return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>N</th>\n",
       "      <th>标号</th>\n",
       "      <th>均方误差</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5</td>\n",
       "      <td>问题 1 (1)</td>\n",
       "      <td>2.465190e-32</td>\n",
       "      <td>[0, 0.2, 0.4, 0.6, 0.8, 1]</td>\n",
       "      <td>[-1, -1.2, -1.4, -1.6, -1.8, -2]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>问题 1 (1)</td>\n",
       "      <td>3.182337e-31</td>\n",
       "      <td>[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0....</td>\n",
       "      <td>[-1, -1.1, -1.2, -1.3, -1.4, -1.5, -1.6, -1.7,...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>20</td>\n",
       "      <td>问题 1 (1)</td>\n",
       "      <td>2.206932e-31</td>\n",
       "      <td>[0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4...</td>\n",
       "      <td>[-1, -1.05, -1.1, -1.15, -1.2, -1.25, -1.3, -1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5</td>\n",
       "      <td>问题 1 (2)</td>\n",
       "      <td>2.569560e-11</td>\n",
       "      <td>[0, 0.2, 0.4, 0.6, 0.8, 1]</td>\n",
       "      <td>[1, 0.8333, 0.7143, 0.625, 0.5556, 0.5]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10</td>\n",
       "      <td>问题 1 (2)</td>\n",
       "      <td>1.282857e-13</td>\n",
       "      <td>[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0....</td>\n",
       "      <td>[1, 0.9091, 0.8333, 0.7692, 0.7143, 0.6667, 0....</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>20</td>\n",
       "      <td>问题 1 (2)</td>\n",
       "      <td>5.366889e-16</td>\n",
       "      <td>[0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4...</td>\n",
       "      <td>[1, 0.9524, 0.9091, 0.8696, 0.8333, 0.8, 0.769...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5</td>\n",
       "      <td>问题 2 (1)</td>\n",
       "      <td>2.023870e+03</td>\n",
       "      <td>[1, 1.4, 1.8, 2.2, 2.6, 3]</td>\n",
       "      <td>[0, 2.608, 8.124, 17.66, 32.55, 54.5]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>10</td>\n",
       "      <td>问题 2 (1)</td>\n",
       "      <td>1.541237e+03</td>\n",
       "      <td>[1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3]</td>\n",
       "      <td>[0, 1.006, 2.614, 4.947, 8.138, 12.33, 17.68, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>20</td>\n",
       "      <td>问题 2 (1)</td>\n",
       "      <td>1.315425e+03</td>\n",
       "      <td>[1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1....</td>\n",
       "      <td>[0, 0.435, 1.006, 1.727, 2.614, 3.682, 4.948, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5</td>\n",
       "      <td>问题 2 (2)</td>\n",
       "      <td>7.459298e-07</td>\n",
       "      <td>[1, 1.4, 1.8, 2.2, 2.6, 3]</td>\n",
       "      <td>[-2, -1.554, -1.384, -1.293, -1.238, -1.2]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10</td>\n",
       "      <td>问题 2 (2)</td>\n",
       "      <td>4.401675e-10</td>\n",
       "      <td>[1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3]</td>\n",
       "      <td>[-2, -1.714, -1.556, -1.455, -1.385, -1.333, -...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>20</td>\n",
       "      <td>问题 2 (2)</td>\n",
       "      <td>8.946298e-14</td>\n",
       "      <td>[1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1....</td>\n",
       "      <td>[-2, -1.833, -1.714, -1.625, -1.556, -1.5, -1....</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>5</td>\n",
       "      <td>问题 3 (1)</td>\n",
       "      <td>3.263086e+05</td>\n",
       "      <td>[0, 0.2, 0.4, 0.6, 0.8, 1]</td>\n",
       "      <td>[0.3333, 2.507, 11.69, 55.95, 275.5, 1372]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>10</td>\n",
       "      <td>问题 3 (1)</td>\n",
       "      <td>4.889478e-01</td>\n",
       "      <td>[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0....</td>\n",
       "      <td>[0.3333, 0.2511, 0.3637, 0.5412, 0.7404, 0.946...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>20</td>\n",
       "      <td>问题 3 (1)</td>\n",
       "      <td>4.815643e-01</td>\n",
       "      <td>[0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4...</td>\n",
       "      <td>[0.3333, 0.1644, 0.1666, 0.2331, 0.3237, 0.423...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>5</td>\n",
       "      <td>问题 3 (2)</td>\n",
       "      <td>1.697642e+06</td>\n",
       "      <td>[0, 0.2, 0.4, 0.6, 0.8, 1]</td>\n",
       "      <td>[-1, -4.803, -24.62, -124.5, -624.7, -3126]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>10</td>\n",
       "      <td>问题 3 (2)</td>\n",
       "      <td>3.852943e-01</td>\n",
       "      <td>[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0....</td>\n",
       "      <td>[-1, -0.2335, 0.08744, 0.2583, 0.3767, 0.4748,...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>20</td>\n",
       "      <td>问题 3 (2)</td>\n",
       "      <td>2.209629e-01</td>\n",
       "      <td>[0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4...</td>\n",
       "      <td>[-1, -0.325, -0.04079, 0.0967, 0.1789, 0.24, 0...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>5</td>\n",
       "      <td>问题 3 (3)</td>\n",
       "      <td>3.617925e+02</td>\n",
       "      <td>[0, 0.2, 0.4, 0.6, 0.8, 1]</td>\n",
       "      <td>[0, 0.2986, 0.9272, 2.835, 10.71, 47.94]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>10</td>\n",
       "      <td>问题 3 (3)</td>\n",
       "      <td>9.913683e-06</td>\n",
       "      <td>[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0....</td>\n",
       "      <td>[0, 0.1121, 0.2451, 0.4018, 0.5841, 0.7938, 1....</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>20</td>\n",
       "      <td>问题 3 (3)</td>\n",
       "      <td>1.212561e-08</td>\n",
       "      <td>[0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4...</td>\n",
       "      <td>[0, 0.0526, 0.1104, 0.1737, 0.2427, 0.3178, 0....</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     N        标号          均方误差  \\\n",
       "0    5  问题 1 (1)  2.465190e-32   \n",
       "1   10  问题 1 (1)  3.182337e-31   \n",
       "2   20  问题 1 (1)  2.206932e-31   \n",
       "3    5  问题 1 (2)  2.569560e-11   \n",
       "4   10  问题 1 (2)  1.282857e-13   \n",
       "5   20  问题 1 (2)  5.366889e-16   \n",
       "6    5  问题 2 (1)  2.023870e+03   \n",
       "7   10  问题 2 (1)  1.541237e+03   \n",
       "8   20  问题 2 (1)  1.315425e+03   \n",
       "9    5  问题 2 (2)  7.459298e-07   \n",
       "10  10  问题 2 (2)  4.401675e-10   \n",
       "11  20  问题 2 (2)  8.946298e-14   \n",
       "12   5  问题 3 (1)  3.263086e+05   \n",
       "13  10  问题 3 (1)  4.889478e-01   \n",
       "14  20  问题 3 (1)  4.815643e-01   \n",
       "15   5  问题 3 (2)  1.697642e+06   \n",
       "16  10  问题 3 (2)  3.852943e-01   \n",
       "17  20  问题 3 (2)  2.209629e-01   \n",
       "18   5  问题 3 (3)  3.617925e+02   \n",
       "19  10  问题 3 (3)  9.913683e-06   \n",
       "20  20  问题 3 (3)  1.212561e-08   \n",
       "\n",
       "                                                    x  \\\n",
       "0                          [0, 0.2, 0.4, 0.6, 0.8, 1]   \n",
       "1   [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0....   \n",
       "2   [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4...   \n",
       "3                          [0, 0.2, 0.4, 0.6, 0.8, 1]   \n",
       "4   [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0....   \n",
       "5   [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4...   \n",
       "6                          [1, 1.4, 1.8, 2.2, 2.6, 3]   \n",
       "7   [1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3]   \n",
       "8   [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1....   \n",
       "9                          [1, 1.4, 1.8, 2.2, 2.6, 3]   \n",
       "10  [1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3]   \n",
       "11  [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1....   \n",
       "12                         [0, 0.2, 0.4, 0.6, 0.8, 1]   \n",
       "13  [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0....   \n",
       "14  [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4...   \n",
       "15                         [0, 0.2, 0.4, 0.6, 0.8, 1]   \n",
       "16  [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0....   \n",
       "17  [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4...   \n",
       "18                         [0, 0.2, 0.4, 0.6, 0.8, 1]   \n",
       "19  [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0....   \n",
       "20  [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4...   \n",
       "\n",
       "                                                    y  \n",
       "0                    [-1, -1.2, -1.4, -1.6, -1.8, -2]  \n",
       "1   [-1, -1.1, -1.2, -1.3, -1.4, -1.5, -1.6, -1.7,...  \n",
       "2   [-1, -1.05, -1.1, -1.15, -1.2, -1.25, -1.3, -1...  \n",
       "3             [1, 0.8333, 0.7143, 0.625, 0.5556, 0.5]  \n",
       "4   [1, 0.9091, 0.8333, 0.7692, 0.7143, 0.6667, 0....  \n",
       "5   [1, 0.9524, 0.9091, 0.8696, 0.8333, 0.8, 0.769...  \n",
       "6               [0, 2.608, 8.124, 17.66, 32.55, 54.5]  \n",
       "7   [0, 1.006, 2.614, 4.947, 8.138, 12.33, 17.68, ...  \n",
       "8   [0, 0.435, 1.006, 1.727, 2.614, 3.682, 4.948, ...  \n",
       "9          [-2, -1.554, -1.384, -1.293, -1.238, -1.2]  \n",
       "10  [-2, -1.714, -1.556, -1.455, -1.385, -1.333, -...  \n",
       "11  [-2, -1.833, -1.714, -1.625, -1.556, -1.5, -1....  \n",
       "12         [0.3333, 2.507, 11.69, 55.95, 275.5, 1372]  \n",
       "13  [0.3333, 0.2511, 0.3637, 0.5412, 0.7404, 0.946...  \n",
       "14  [0.3333, 0.1644, 0.1666, 0.2331, 0.3237, 0.423...  \n",
       "15        [-1, -4.803, -24.62, -124.5, -624.7, -3126]  \n",
       "16  [-1, -0.2335, 0.08744, 0.2583, 0.3767, 0.4748,...  \n",
       "17  [-1, -0.325, -0.04079, 0.0967, 0.1789, 0.24, 0...  \n",
       "18           [0, 0.2986, 0.9272, 2.835, 10.71, 47.94]  \n",
       "19  [0, 0.1121, 0.2451, 0.4018, 0.5841, 0.7938, 1....  \n",
       "20  [0, 0.0526, 0.1104, 0.1737, 0.2427, 0.3178, 0....  "
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 运行所有并且返回结果表格\n",
    "def run_all():\n",
    "    all_data = [run(i) for i in range(len(global_args))]\n",
    "    all = []\n",
    "    for d in all_data:\n",
    "        all.extend(d)\n",
    "    # 重新格式化为字符串\n",
    "    all = [{\n",
    "            'N': d['N'],\n",
    "            '标号': d['标号'],\n",
    "            '均方误差': d['均方误差'],\n",
    "            'x': [f\"{x:.4g}\" for x in d['x']],\n",
    "            'y': [f\"{x:.4g}\" for x in d['y']],\n",
    "        } for d in all]\n",
    "    return DataFrame(all)\n",
    "\n",
    "\n",
    "run_all()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*为防止输出 PDF 时表格格式被破坏，在此放入上方表格的图片。*\n",
    "\n",
    "![result_table](./imgs/lab03_result_fixed.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实验结果\n",
    "\n",
    "> 准确规范地给出各个实验题目的结果，并对相应的思考题给出正确合理的回答与说明。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实验数据结果如上表所示。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**思考题：**\n",
    "\n",
    "1. *对实验 1，数值解和解析解相同吗？为什么？试加以说明。*\n",
    "    \n",
    "    在误差范围内基本可以认为相同。由上表可知，对问题 1，当 $N = 20$ 时，其结果和标准值的均方误差均小于 $10^{-15}$，都是非常小的，所以在误差范围内可以认为数值解和解析解相同。\n",
    "\n",
    "2. *对实验 2，N 越大越精确吗？试加以说明。*\n",
    "\n",
    "    在实验 2 的数据中，随着 $N$ 的增大，其均方误差越来越小，所以对实验二，$N$ 越大越精确。\n",
    "\n",
    "3. *对实验 3，N 较小会出现什么现象？试加以说明。*\n",
    "\n",
    "    在实验 3 的数据中，当 $N$ 较小时，其均方误差非常大，达到 $10^2$ 甚至 $10^6$。"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "02580f3b40faffdca494a56716cab34cc74dfb88737f96a81a04d7c61a2f2be5"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
